package com.fzj.ivos_serve.user.service.impl;

import com.fzj.ivos_serve.base.exception.ServiceException;
import com.fzj.ivos_serve.base.response.StatusCode;
import com.fzj.ivos_serve.user.mapper.UserMapper;
import com.fzj.ivos_serve.user.pojo.dto.UserLoginParam;
import com.fzj.ivos_serve.user.pojo.dto.UserQuery;
import com.fzj.ivos_serve.user.pojo.dto.UserSaveParam;
import com.fzj.ivos_serve.user.pojo.entity.User;
import com.fzj.ivos_serve.user.pojo.vo.UserVO;
import com.fzj.ivos_serve.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    public UserVO login(UserLoginParam userLoginParam){
        log.debug("登录业务，参数：{}",userLoginParam);

        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userLoginParam.getPassword().equals(userVO.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public List<UserVO> selectUserList(UserQuery userQuery) {

        log.debug("查询用户列表，参数:{}",userQuery);
        List<UserVO> userVOList = userMapper.select(userQuery);
        return  userVOList;
    }

    @Override
    public void save(UserSaveParam userSaveParam) {
        log.debug("保存用户信息业务，参数：{}",userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(userSaveParam.getId()== null){
            //新增用户业务
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{
            //修改用户信息业务
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public UserVO selectById(Long id) {
        log.debug("根据id查询用户参数：{}",id);
        UserVO userVO = userMapper.selectById(id);
        return userVO;
    }

    @Override
    public void deleteById(Long id) {
        log.debug("根据id删除用户业务，参数：{}",id);
        userMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改用户状态业务，参数：{}，{}",id,status);
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void resetPassword(Long id) {
        log.debug("重置密码业务，参数：{}",id);
        User user = new User();
        user.setPassword("123456");
        user.setId(id);
        user.setUpdateTime(new Date());
        userMapper.resetPassword(user);
    }

}
